package com.lee.mybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lee.mybatisplus.entity.User;
import com.lee.mybatisplus.service.UserSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author jackielee
 * @version 1.0
 * @description UserController
 * @date 2021/4/9
 **/
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserSerivce userSerivce;

    @GetMapping("/test")
    public Object test(int pn, int size){
        //User user = userSerivce.getById(1)
        User user = userSerivce.getOne(new QueryWrapper<User>().eq("username","lee"));
        System.out.println(user);

//        ServletRequestUtils.getIntParameter(req,"id");
        Page p = new Page(pn, size);
        IPage<User> users = userSerivce.page(p, new QueryWrapper<User>().isNotNull("id").orderByDesc("created"));
        return users;

        // 嵌套查询
//        IPage page1 = userService.page(p, new QueryWrapper<User>()
//                .inSql("id", "SELECT user_id FROM user_collection where post_id = 1")
//        );
//
//        // 增删改都是差不多，直接调用api
//        user.setUsername("lee1");
//        userService.saveOrUpdate(user);
//        userService.remove(new QueryWrapper<User>().eq("username", "lee"));
    }
}
